// 上面这个代码处理过度动画（默认加上不用管）
document.addEventListener("DOMContentLoaded", () => {
  setTimeout(() => {
    document.body.classList.add("sidenav-pinned");
    document.body.classList.add("ready");
  }, 200);
});

// 每个页面都会导入该js 统一的设置写这里即可

//封装轻弹窗
const toastBox = document.querySelector("#myToast");
const toast = new bootstrap.Toast(toastBox);
const tip = (msg) => {
  toastBox.querySelector(".toast-body").innerHTML = msg;
  toast.show();
};

//设置axios请求的基地址
axios.defaults.baseURL = 'http://ajax-api.itheima.net';

axios.interceptors.request.use(
  function (config) {
    config.headers.Authorization = localStorage.getItem("token");
    // 在发送请求之前做些什么
    return config;
  },
  function (error) {
    tip("我错了");
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  function (response) {
  const {data:res} = response
    return res;
  },
  function (error) {
    const status = error.response.status
    if (status === 401) {
      return window.location.href = '/login.html'
    }
    return Promise.reject(error)
  }
);

const username = document.querySelector('#username');
username&&(username.innerText = localStorage.getItem('username'))
const delBtn = document.querySelector('#logout');
delBtn&&delBtn.addEventListener('click', function () {
  localStorage.removeItem('username')
  localStorage.removeItem('token')
  window.location.href='/login.html'
});